package com.takeshequ.regiest;

import java.util.HashMap;
import java.util.Map;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.andreabaccega.widget.FormEditText;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.takeshequ.BasicActivity;
import com.takeshequ.Constants;
import com.takeshequ.R;
import com.takeshequ.ResponseBody;
import com.takeshequ.main.MainFragmentActivity;

public class RegisterActivity extends BasicActivity {
	private ImageView checkImageView;
	private LinearLayout checkImageLayout;
	private FormEditText phonenoText, nickNameText, pwdText, verifyCodeText;
	private Button registerButton;
	private TextView titleView;
	private int step = 1;

	@ViewInject(id = R.id.register_get_verifycode_layout)
	View verifyCodeView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		setViews();

	}

	private void setViews() {
		checkImageLayout = (LinearLayout) findViewById(R.id.register_check_layout);
		checkImageView = (ImageView) findViewById(R.id.register_check_view);
		phonenoText = (FormEditText) findViewById(R.id.register_phoneno);
		nickNameText = (FormEditText) findViewById(R.id.register_nickName);
		verifyCodeText = (FormEditText) findViewById(R.id.register_verifycode);
		pwdText = (FormEditText) findViewById(R.id.register_pwd);
		registerButton = (Button) findViewById(R.id.register_btn);
		// getVerifyCodeButton = (ImageButton)
		// findViewById(R.id.register_get_verifycode_btn);
		titleView = (TextView) findViewById(R.id.top_title);
		titleView.setText("注册");
	}

	private void setEvent() {
		checkImageLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (checkImageView.isShown()) {
					checkImageView.setVisibility(View.INVISIBLE);
				} else {
					checkImageView.setVisibility(View.VISIBLE);
				}
			}
		});
		registerButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (step == 1) {
					if (phonenoText.testValidity()
							&& nickNameText.testValidity()
							&& pwdText.testValidity()) {

						getVerifyCode(phonenoText.getText().toString(),
								nickNameText.getText().toString(), pwdText
										.getEditableText().toString());
					}
				} else {
					if (phonenoText.testValidity()
							&& nickNameText.testValidity()
							&& pwdText.testValidity()
							&& verifyCodeText.testValidity()) {
						register(phonenoText.getText().toString(), nickNameText
								.getText().toString(), pwdText
								.getEditableText().toString(), verifyCodeText
								.getEditableText().toString());
					}
				}

			}
		});

	}

	private void store(String phoneno, String nickName, String pwd,
			String memberId) {
		sharedPreferencesUtils.setNickName(nickName);
		sharedPreferencesUtils.setPhone(phoneno);
		sharedPreferencesUtils.setPwd(pwd);
		sharedPreferencesUtils.setMemberId(memberId);
	}

	@Override
	protected void onResume() {
		super.onResume();
		setEvent();
	}

	private void getVerifyCode(final String phoneno, final String nickName,
			final String pwd) {
		AsyncHttpClient client = new AsyncHttpClient();
		Map<String, String> params = new HashMap<String, String>();
		params.put("nickName", nickName);
		params.put("loginName", phoneno);
		params.put("password", pwd);
		client.post(Constants.domainAndPort + "/getverifycode",
				new RequestParams(params), new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String response) {
						ResponseBody responseBody = gson.fromJson(response,
								ResponseBody.class);
						store(phoneno, nickName, pwd, responseBody.getState());
						if (responseBody.getState().equals("1")) {
							registerButton.setText("注册");
							step = 2;
							verifyCodeView.setVisibility(View.VISIBLE);
						} else {
							Toast.makeText(RegisterActivity.this,
									responseBody.getDesc(), 2000).show();
						}

					}
				});
	}

	private void register(final String phoneno, final String nickName,
			final String pwd, final String verfiCode) {
		AsyncHttpClient client = new AsyncHttpClient();

		Map<String, String> params = new HashMap<String, String>();
		params.put("nickName", nickName);
		params.put("loginName", phoneno);
		params.put("password", pwd);
		params.put("verifyCode", verfiCode);
		params.put("appSource", "A");
		params.put("appVersion", "1");
		params.put("regSource", "mobile");
		client.post(Constants.domainAndPort + "/reg",
				new RequestParams(params), new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String response) {
						ResponseBody responseBody = gson.fromJson(response,
								ResponseBody.class);

						if (responseBody.getState().equals("1")) {
							store(phoneno, nickName, pwd, responseBody
									.getMember().getMemberId());
							JPushInterface.setAliasAndTags(
									RegisterActivity.this, responseBody
											.getMember().getMemberId(), null);
							Intent intent = new Intent(RegisterActivity.this,
									MainFragmentActivity.class);
							startActivity(intent);

						} else {
							Toast.makeText(RegisterActivity.this,
									responseBody.getDesc(), 2000).show();
						}

					}
				});

	}
}
